import axios from "../../http/axios";
import apis from "../../http/apis.js";
export default{
    namespaced: true,
    state:{
        pageData: {
            salespersons: [],

        },
        pages:0,
        total: 0,
        pageSize: 3,
        currentPage: 1,
        value:'',
        PersonalchangeData: {

        }
    },
    mutations:{
        setPersonal(state,data) {
            state.pageData.salespersons = data.salespersons;
            state.pages=data.pages;
            state.total=data.total;
        },
        setPageSize(state,pageSize){
            state.pageSize=pageSize;    
        },
        setChangeData(state, data) {
            state.PersonalchangeData = data
        },
        setCurrentPage(state,currentPage){
            if(currentPage>=1&&currentPage<=state.pages){
                state.currentPage=currentPage
            }
            
        }
    },
    actions:{
        async getSalespersons(context,value) {
            const { data } = await axios({
                url:'/salespersons/getSalespersons',
                method: 'get',
                params: {
                    pageSize:context.state.pageSize,
                    currentPage:context.state.currentPage,
                    value
                },
            });
            if (data.status) {

                context.commit('setPersonal', data.data);
                console.log("获取到了销售员数据",data.data);
                
            } else {

                alert('获取学生数据失败')
            }
        },
        async deleteSalespersons(context, salespersonsId) {
            const { data } = await apis.salespersons.deleteSalespersons({
                salespersonsId
            });
            if (data.status) {
                alert('删除该客户信息成功');
                context.dispatch('getSalespersons')


            } else {
                alert('删除失败，请联系管理人员')
            }
        },
        async updateSalespersons(context, personalData) {
            
            const { data } = await apis.salespersons.updateSalespersons({
                personalData
            })
            if (data.status) {
                alert('修改销售员信息成功');
                context.dispatch('getSalespersons')
            } else {
                alert('修改失败')
            }

        },
        async getSalespersonsById(context,_id) {
            // const {data}=await axios({
            //     url:'/companies/getCompaniesById',
            //     method:'post',
            //     data:{
            //         _id:_id.substring(1,25)
            //     }
            // });
            const { data } = await apis.salespersons.getSalespersonsById({
                _id:_id.substring(1, 25)
            })
            if (data.status) {
                console.log('销售员数据',data.data[0]);
                context.commit('setChangeData',data.data[0]);
                
            } else {
                console.log(911, data);
            }
        },
        async addSalespersons(context,salespersons) {
            const names = salespersons.names;
            const account = salespersons.account;
            const password = salespersons.password;
            const phonenum = salespersons.phonenum;
           
           

            const { data } = await axios({
                url: "/salespersons/addSalespersons",
                method: 'post',
                data: {
                    phonenum,password,account,names
                }
            });
            if (data.status) {
                alert('新增销售员成功');

            } else {
                alert('新增失败')
            }
        },
    }
}